import { routerRedux } from 'dva'
import modelEnhance from '@/utils/modelEnhance'
import { getCoursewareList } from '../service'

export default modelEnhance({
  namespace: 'courseware',

  state: {
    data: {},
    type: 'list',
    isshow: false,
    id: null
  },

  effects: {
    * getCoursewareList ({ payload }, { call, put }) {
      const { data } = yield call(getCoursewareList, payload)
      yield put({
        type: 'save',
        payload: {
          data: data
        }
      })
      yield put({
        type: 'getEmployees'
      })
    },
    * changeType ({payload}, { call, put }) {
      yield put({
        type: 'change',
        payload: {
          type: payload,
          isshow: payload === 'show'
        }
      })
      if (payload && (payload === 'edit' || payload == 'show')) {
        yield put(routerRedux.replace('/EditCourseware'))
      } else if (payload && payload === 'add') {
        yield put(routerRedux.replace('/AddCourseware'))
      } else {
        yield put(routerRedux.replace('/'))
      }
    }
  },
  reducers: {
    save (state, { payload }) {
      return {
        ...state,
        data: payload.data,
      }
    },
    change (state, { payload }) {
      return {
        ...state,
        type: payload.type,
        isshow: payload.isshow,
        id: payload.id
      }
    }
  }
})
